//
//  MBProgressHUD+Extension.swift
//  Asman
//
//  Created by 李旭峰 on 5/20/19.
//  Copyright © 2019 李旭峰. All rights reserved.
//

import Foundation
import MBProgressHUD

extension MBProgressHUD {
    
    ///显示信息
    ///
    /// - Parameters:
    ///   - text: 信息内容
    ///   - icon: 图标
    ///   - view: 显示视图
    class func show(text:String, icon:String, view:UIView?) {
        var newView = view
        if newView == nil {
            newView = UIApplication.shared.windows.last
        }
        let hub = MBProgressHUD.showAdded(to: newView!, animated: true)
        hub?.labelText = text
        hub?.labelFont = FontAsset.PingFangSC_Regular.size(16 + (ceil(kScreenWidth/375.0) - 1)*2)
        hub?.mode = .customView
        hub?.removeFromSuperViewOnHide = true
        hub?.hide(true, afterDelay: 1.5)
        hub?.customView = UIImageView(image: UIImage(named: icon))
//        hub.contentColor = UIColor.white
//        hub.bezelView.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.9)
//        hub.customView = UIImageView(image: UIImage(named: icon))
    }
    
    /// 显示成功新
    ///
    /// - Parameter success: 信息内容
    class func showSuccess(success:String) {
        self.showSuccess(success: success, toView: nil)
    }
    
    
    /// 显示成功信息
    ///
    /// - Parameters:
    ///   - success: 信息内容
    ///   - view: 显示信息的视图
    class func showSuccess(success:String, toView view:UIView?) {
        self.show(text: success, icon: "", view: view)
    }
    
    /// 显示错误信息
    ///
    /// - Parameter error: 错误信息
    class func showError(error:String) {
        self.showError(error: error, toView: nil)
    }
    
    /// 显示错误信息
    ///
    /// - Parameters:
    ///   - error: 错误信息内容
    ///   - view: 需要显示信息的视图
    class func showError(error:String, toView view:UIView?) {
        self.show(text: error, icon: "", view: view)
    }
    
    //    class func showMessage(message:String) -> MBProgressHUD{
    //        return self.showMessage(message: message, toView: nil)
    //    }
    
    class func showMessage(message:String?, toView view:UIView?){
        var newView = view
        if newView == nil {
            newView = UIApplication.shared.windows.last
        }
        let hub = MBProgressHUD.showAdded(to: view!, animated: true)
        hub?.labelText = message
        hub?.labelFont = FontAsset.PingFangSC_Regular.size(16 + (ceil(kScreenWidth/375.0) - 1)*2)
//        hub.contentColor = UIColor.white
//        hub.bezelView.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.9)
        hub?.removeFromSuperViewOnHide = true
    }
    
    
    /// 手动关闭MBProgressHUD
    class func hideHUD() {
        self.hideHUDForView(view: nil)
    }
    
    
    /// 手动关闭MBProgressHUD
    ///
    /// - Parameter view:显示MBProgressHUD的视图
    class func hideHUDForView(view:UIView?) {
        var newView = view
        if newView == nil {
            newView = UIApplication.shared.windows.last
        }
        self.hide(for: newView!, animated: true)
    }
    
    
}

